<?php
class Modules extends MY_Controller{
	var $beforeFilter = array(
			'initAdmin' => array(
				'only' => array('index','admin','add','remove','edit','save'),
			),
		);

	function Modules()
	{
		parent::MY_Controller();
		$this->load->model('Module');
		$this->load->helper(array('url','system'));
	}
	
	function index()
	{
		$this->assign['modules'] = $this->listAll();
		$this->display();
	}

	function options(){
		$this->assign['result'] = $this->listAll();
		$this->display();
	}

	function listAll(){
		$result = $this->Module->listModule();
		$options = cat_options(0, $result);
		return $options;
	}

	function save(){
		$redirect = $this->getRedirect('/index.php/modules/admin');
		$rules['name']	= "required";
		if ($this->validation($rules) == FALSE)
		{
			$this->showMessage('error_wrong_input'); 
		}
		$this->Module->save($_POST);
		$this->showMessage('mes_success',$redirect);
	}

	function admin(){
		$this->assign['modules'] = $this->listAll();
		$this->display();
	}

	function doEdit($id=null){
		$this->load->helper('system');
		$this->load->helper('form');
		$redirect = $this->getRedirect('module_admin');
		$result = $this->Module->listModule();
		if($id>0){
			$module  = getByCond($result,array('id'=>$id));
			$module  = $module[0];
			$this->assign['module'] = $module;
		}
		$options = cat_list(cat_options(0, $result),0, $module['parent_id'],true, 3,true);
		$this->assign['options'] = $options;
		$this->assign['redirect'] = $redirect;
		$this->template = 'modules_save';
		$this->display();
	}

	function add(){
		$this->doEdit();
	}

	function edit($id){
		$this->doEdit($id);
	}

	function remove($id=null){
		$redirect = $this->getRedirect('/index.php/modules/admin');
		$id = empty($id) ? $_REQUEST['id'] : $id;
		$this->Module->removeByPks($id);
		$this->showMessage('mes_success',$redirect);
	}
}